﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintianDepartment : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public MstDepartmentEntity SelectedDepartment { get; set; }
        #endregion

        #region Constructor
        public frmMaintianDepartment()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void SetFields()
        {
            if (SelectedDepartment == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(SelectedDepartment.ParentId) == false)
            {
                MstDepartmentEntity department = MstDepartmentBs.GetMstDepartmentByID(SelectedDepartment.ParentId);
                txtParentDepartment.Text = department.DepartmentName;
                txtParentDepartment.Tag = department;
            }
            txtDepartmentCode.Text = SelectedDepartment.DepartmentCode;
            txtDepartmentName.Text = SelectedDepartment.DepartmentName;
            if (string.IsNullOrEmpty(SelectedDepartment.Manager) == false)
            {
                MstPersonEntity manager = MstPersonBs.GetMstPersonByID(SelectedDepartment.Manager);
                txtManager.Text = manager.NameCn;
                txtManager.Tag = manager;
            }
            cmbDeleteFlag.SetSelectedValue(SelectedDepartment.DeleteFlag);
            txtSequence.Text = SelectedDepartment.Sequence.ToString();
            txtRemark.Text = SelectedDepartment.Description;
        }

        private void SetEntity()
        {
            if (SelectedDepartment == null)
            {
                SelectedDepartment = new MstDepartmentEntity();
                SelectedDepartment.DepartmentId = Guid.NewGuid().ToString();
            }
            SelectedDepartment.ParentId = null;
            if (txtParentDepartment.Tag != null && txtParentDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                SelectedDepartment.ParentId = ((MstDepartmentEntity)txtParentDepartment.Tag).DepartmentId;
            }
            SelectedDepartment.DepartmentCode = txtDepartmentCode.Text;
            SelectedDepartment.DepartmentName = txtDepartmentName.Text;
            SelectedDepartment.Manager = null;
            if (txtManager.Tag != null && txtManager.Tag.GetType() == typeof(MstPersonEntity))
            {
                SelectedDepartment.Manager = ((MstPersonEntity)txtManager.Tag).PersonId;
            }
            SelectedDepartment.DeleteFlag = CommonFunctions.ToInt32(cmbDeleteFlag.SelectedValue);
            SelectedDepartment.Sequence = CommonFunctions.ToInt32(txtSequence.Text);
            SelectedDepartment.Description = txtRemark.Text;
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(txtDepartmentCode.Text))
            {
                MessageBox.Show("部门编码不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtDepartmentName.Text))
            {
                MessageBox.Show("部门名称不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private bool Save()
        {
            try
            {
                if (CheckData() == false)
                {
                    return false;
                }
                SetEntity();
                if (ThisActionType == ActionType.New)
                {
                    bool isSuccessful = MstDepartmentBs.InsertMstDepartment(SelectedDepartment);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                else if (ThisActionType == ActionType.Modify)
                {
                    bool isSuccessful = MstDepartmentBs.UpdateMstDepartment(SelectedDepartment);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        #region Event
        private void frmMaintianDepartment_Load(object sender, EventArgs e)
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbDeleteFlag, typeof(DeleteFlag), typeof(int), false);
            SetFields();
        }

        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtParentDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtParentDepartment.Text = frm.SelectedDepartment.DepartmentName;
                txtParentDepartment.Tag = frm.SelectedDepartment;
            }
            else
            {
                txtParentDepartment.Text = "";
                txtParentDepartment.Tag = null;
            }
        }

        private void txtManager_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtManager.Text = frm.SelectedPerson.NameCn;
                txtManager.Tag = frm.SelectedPerson;
            }
            else
            {
                txtManager.Text = "";
                txtManager.Tag = null;
            }
        }

        #endregion
    }
}
